/**
 * 邮箱
 * @param {*} s
 */
function isEmail (s) {
  return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
}

/**
 * 手机号码
 * @param {*} s
 */
function isMobile (s) {
  return /^1[0-9]{10}$/.test(s)
}

/**
 * 电话号码
 * @param {*} s
 */
function isPhone (s) {
  return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s)
}

/**
 * URL地址
 * @param {*} s
 */
function isURL (s) {
  return /^http[s]?:\/\/.*/.test(s)
}

/**
 * 判断值是否为空
 * @param val: 传入的值
 */
function isEmpty(val){
	if(val === "" || val === undefined || val === null || trim(val) === ""
		|| JSON.stringify(val) === "{}" || JSON.stringify(val) === "[]"){
		return true
	}
	return false
}
 /**
 * 判断值是否不为空
 * @param val: 传入的值
*/
function isNotEmpty(val){
	if(val === "" || val === undefined || val === null || trim(val) === ""
		|| JSON.stringify(val) === "{}" || JSON.stringify(val) === "[]"){
		return false
	}
	return true
}

/**
 * 去除首尾空格
 * @param {Object} val: 传入的值
 */
function trim(val) {
	if(val != null && typeof val === 'string') {
		val = val.replace(/(^\s*)|(\s*$)/g,"")
	}
	return val
}

/**
 * 获取日期差值
 */
function getDateDiffer(startDate, endDate){
	var day = 24 * 60 * 60 * 1000
	var dateDifference = 0
	if (this.isNotEmpty(startDate) && this.isNotEmpty(endDate)) {
		if (startDate === endDate) {
			dateDifference = 1
		} else {
			var dateArrStart = startDate.split("-")
			var dateArrEnd = endDate.split("-")
			var checkDateStart = new Date()
			var checkDateEnd = new Date()
			var checkTimeSatrt = checkDateStart.setFullYear(dateArrStart[0], dateArrStart[1] - 1, dateArrStart[2])
			var checkTimeEnd = checkDateEnd.setFullYear(dateArrEnd[0], dateArrEnd[1] - 1, dateArrEnd[2])
			dateDifference = Math.abs((checkTimeEnd - checkTimeSatrt) / day).toFixed(0)
		}
	}
	return dateDifference

}

//将后台返回的list数组组装成树 this.util.treeDataTranslate(data.data,'deptId','parentId')
function treeDataTranslate (data, id, pid) {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
	temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
	if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
	  if (!temp[data[k][pid]]['children']) {
		temp[data[k][pid]]['children'] = []
	  }
	  if (!temp[data[k][pid]]['_level']) {
		temp[data[k][pid]]['_level'] = 1
	  }
	  data[k]['_level'] = temp[data[k][pid]]._level + 1
	  temp[data[k][pid]]['children'].push(data[k])
	} else {
	  res.push(data[k])
	}
  }
  return res
}
//时间格式化YYYY-MM-DD
function timeFormatYYYYMMDD(time) { // 时间格式化 2019-09-08
	return `${time.getFullYear()}-${time.getMonth() + 1 >= 10 ? (time.getMonth() + 1) : '0' + (time.getMonth() + 1)}-${time.getDate() >= 10 ? time.getDate() : '0' + time.getDate()}`;
}
//时间格式化YYYY-MM-DD HH:mm:ss
function timeFormatYYYYMMDDHHMMSS(time) { // 时间格式化 2019-09-08 12:10:00
	return `${time.getFullYear()}-${time.getMonth() + 1 >= 10 ? (time.getMonth() + 1) : '0' + (time.getMonth() + 1)}-${time.getDate() >= 10 ? time.getDate() : '0' + time.getDate()} ${time.getHours() >= 10 ? time.getHours():'0' + time.getHours()}:${time.getMinutes()>=10?time.getMinutes():'0'+time.getMinutes()}:${time.getSeconds() >= 10 ? time.getSeconds():'0' + time.getSeconds()}`;
}
//去掉html中的标签，只保留文本
function htmlToString(htmlStr){
	return htmlStr.replace(/<\/?.+?\/?>/g,'')
}
export default{
    isEmail,
    isMobile,
    isPhone,
    isURL,
	isEmpty,
	isNotEmpty,
	trim,
	getDateDiffer,
	treeDataTranslate,
	timeFormatYYYYMMDD,
	timeFormatYYYYMMDDHHMMSS,
	htmlToString
}
